﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Adams.Public.DataAccess.Web.Donate
{
    public static class Payment
    {
        public static Adams.Public.DataTransfer.Web.Donate.Payment.PageLoad PageLoad()
        {
            var data = new Adams.Public.DataTransfer.Web.Donate.Payment.PageLoad();
            data.Buckets = new Shared.Entities.Donations.BucketCollection();

            using (SqlDataReader dr = DB.RunDataReader(@"
SELECT * 
FROM Donations.Buckets
ORDER BY Name
",
                   CommandType.Text))
            {
                while (dr.Read())
                {
                    var bucket = new Shared.Entities.Donations.Bucket();
                    bucket.ID = dr["BucketID"].DbInt();
                    bucket.Name = dr["Name"].DbString();
                    bucket.IsDefault = dr["IsDefault"].DbBool();
                    data.Buckets.Add(bucket);
                }
            }

            return data;
        }

        public static int SaveDonation(DataTransfer.Web.Donate.Payment.SaveInput input)
        {
            int donationid = DB.RunScalar(@"
INSERT INTO Donations.Quick (
    BucketID
    ,Amount
    ,Title
    ,FirstName
    ,LastName
    ,Email
    ,Street
    ,Street2
    ,City
    ,State
    ,ZipCode
    ,Phone
    ,CardHolderName
    ,CardNumber
    ,CardType
    ,CardLast4Digits
    ,CardExpireMonth
    ,CardExpireYear
    ,CardSecurityCode
    ,EnteredOn
    ,StatusID)
VALUES (
    @BucketID
    ,@Amount
    ,@Title
    ,@FirstName
    ,@LastName
    ,@Email
    ,@Street
    ,@Street2
    ,@City
    ,@State
    ,@ZipCode
    ,@Phone
    ,@CardHolderName
    ,@CardNumber
    ,@CardType
    ,@CardLast4Digits
    ,@CardExpireMonth
    ,@CardExpireYear
    ,@CardSecurityCode
    ,@EnteredOn
    ,@StatusID)
SELECT SCOPE_IDENTITY();
    ", CommandType.Text,
                    DB.CreateParameter("@BucketID", input.BucketID.ToInt()),
                    DB.CreateParameter("@Amount", input.Amount),
                    DB.CreateParameter("@Title", input.Title.ToNString()),
                    DB.CreateParameter("@FirstName", input.FirstName),
                    DB.CreateParameter("@LastName", input.LastName),
                    DB.CreateParameter("@Email", input.Email),
                    DB.CreateParameter("@Street", input.Street),
                    DB.CreateParameter("@Street2", input.Street2.ToNString()),
                    DB.CreateParameter("@City", input.City),
                    DB.CreateParameter("@State", input.State),
                    DB.CreateParameter("@ZipCode", input.ZipCode),
                    DB.CreateParameter("@Phone", input.Phone),
                    DB.CreateParameter("@CardHolderName", input.CardHolderName),
                    DB.CreateParameter("@CardNumber", input.CardNumber),
                    DB.CreateParameter("@CardType", input.CardType),
                    DB.CreateParameter("@CardLast4Digits", input.Last4Digits),
                    DB.CreateParameter("@CardExpireMonth", input.CardExpireMonth),
                    DB.CreateParameter("@CardExpireYear", input.CardExpireYear),
                    DB.CreateParameter("@CardSecurityCode", input.CardSecurityCode),
                    DB.CreateParameter("@EnteredOn", input.EnteredOn),
                    DB.CreateParameter("@StatusID", input.StatusID)).DbInt();

            return donationid;
        }

        public static Adams.Public.DataTransfer.Web.Donate.Confirm.PageLoad ConfirmPageLoad(DataTransfer.Web.Donate.Confirm.PageLoadInput input)
        {
            var data = new Adams.Public.DataTransfer.Web.Donate.Confirm.PageLoad();

            using (SqlDataReader dr = DB.RunDataReader(@"

SELECT B.*
FROM Donations.Quick R INNER JOIN Donations.Buckets B ON R.BucketID = B.BucketID
WHERE R.DonationID = @DonationID

SELECT R.*
FROM Donations.Quick R
WHERE R.DonationID = @DonationID AND StatusID = 1

", CommandType.Text,
    DB.CreateParameter("@DonationID", input.DonationID)))
            {
                data.DonationID = input.DonationID.ToInt();
                dr.Read();
                var bucket = new Shared.Entities.Donations.Bucket()
                {
                    ID = dr["BucketID"].DbInt(),
                    Name = dr["Name"].DbString(),
                    IsDefault = dr["IsDefault"].DbBool(),
                    AllowRecurring = dr["AllowRecurring"].DbBool()
                };

                dr.NextResult();

                dr.Read();
                data.Quick = new Shared.Entities.Donations.Quick()
                {
                    ID = dr["DonationID"].DbInt(),
                    Amount = dr["Amount"].DbDecimal(),
                    Title = dr["Title"].DbString(),
                    FirstName = dr["FirstName"].DbString(),
                    LastName = dr["LastName"].DbString(),
                    Email = dr["Email"].DbString(),
                    Street = dr["Street"].DbString(),
                    Street2 = dr["Street2"].DbString(),
                    City = dr["City"].DbString(),
                    State = dr["State"].DbString(),
                    ZipCode = dr["ZipCode"].DbString(),
                    Phone = dr["Phone"].DbString(),
                    CardHolderName = dr["CardHolderName"].DbString(),
                    CardNumber = dr["CardNumber"].DbString(),
                    CardType = dr["CardType"].DbString(),
                    CardLast4Digits = dr["CardLast4Digits"].DbString(),
                    CardExpireMonth = dr["CardExpireMonth"].DbInt(),
                    CardExpireYear = dr["CardExpireYear"].DbInt(),
                    CardSecurityCode = dr["CardSecurityCode"].DbString(),
                    Bucket = bucket
                };
            }

            return data;
        }

        public static void ConfirmDonation(DataTransfer.Web.Donate.Confirm.SaveInput input)
        {
            DB.Run(@"
 UPDATE Donations.Quick SET StatusID = @StatusID WHERE DonationID = @DonationID AND StatusID = 1
    ", CommandType.Text,
                    DB.CreateParameter("@DonationID", input.DonationID.ToInt()),
                    DB.CreateParameter("@StatusID", input.StatusID));
        }
    }
}